# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 鸢尾花_网格搜索+交叉验证.py
# @Author: dongguangwen
# @Date  : 2025-01-19 19:00
# 0. 导入工具包
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score


# 1.加载数据
data = load_iris()

# 2.数据集划分
x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=22)

# 3.特征预处理
pre = StandardScaler()
x_train = pre.fit_transform(x_train)
x_test = pre.transform(x_test)

# 4.模型实例化+交叉验证+网格搜索
model = KNeighborsClassifier(n_neighbors=1)
params_grid = {'n_neighbors': [4, 5, 7, 9]}
estimator = GridSearchCV(estimator=model, param_grid=params_grid, cv=4)
estimator.fit(x_train, y_train)

print(estimator.best_score_)
print(estimator.best_estimator_)
print(estimator.cv_results_)


model = KNeighborsClassifier(n_neighbors=7)
model.fit(x_train, y_train)

y_pred = model.predict(x_test)
print(accuracy_score(y_test, y_pred))

"""
0.9666666666666668
KNeighborsClassifier(n_neighbors=7)
{'mean_fit_time': array([0.00049871, 0.00074804, 0.00074798, 0.00074786]), 'std_fit_time': array([0.00049871, 0.00043188, 0.00043185, 0.00043178]), 'mean_score_time': array([0.00274271, 0.00224394, 0.00224406, 0.00249338]), 'std_score_time': array([0.00043178, 0.00043185, 0.00043178, 0.00049877]), 'param_n_neighbors': masked_array(data=[4, 5, 7, 9],
             mask=[False, False, False, False],
       fill_value=999999), 'params': [{'n_neighbors': 4}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}], 'split0_test_score': array([1., 1., 1., 1.]), 'split1_test_score': array([0.96666667, 0.96666667, 0.96666667, 0.96666667]), 'split2_test_score': array([0.9       , 0.93333333, 0.93333333, 0.93333333]), 'split3_test_score': array([0.9       , 0.93333333, 0.96666667, 0.93333333]), 'mean_test_score': array([0.94166667, 0.95833333, 0.96666667, 0.95833333]), 'std_test_score': array([0.04330127, 0.02763854, 0.02357023, 0.02763854]), 'rank_test_score': array([4, 2, 1, 2], dtype=int32)}
0.9333333333333333
"""
